<template>
	<Content title="修改密码">
		<view class="login-box">
			<view class="tip">
				<view class="dt">{{ userInfo.realname }}</view>
			</view>
			<view class="from-box">
				<view class="content">
					<u-form labelPosition="top" :model="model" label-width="200" labelAlign="left" :rules="rules"
						ref="uForm">
						<u-form-item label="原密码" prop="formerPassword">
							<u-input v-model="model.formerPassword" type="password" placeholder="请输入密码"
								clearable></u-input>
						</u-form-item>

						<u-form-item label="新密码" prop="password">
							<u-input v-model="model.password" type="password" placeholder="请输入密码" clearable></u-input>
						</u-form-item>

						<u-form-item label="确认新密码" prop="passWordAgain">
							<u-input v-model="model.passWordAgain" type="password" placeholder="请确认新密码"
								clearable></u-input>
						</u-form-item>
					</u-form>
					<view class="but-box">
						<view class="btn">
							<u-button type="primary" @click="submit" text="修改"></u-button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</Content>
</template>

<script>
	import Content from "@/components/content/index.vue";
	import fetch from "@/utils/fetch";
	import {
		setToken,
		setUserInfo,
		getUserInfo
	} from "@/store/user";
	export default {
		components: {
			Content,
		},
		data() {
			const that = this;
			return {
				rules: {
					formerPassword: {
						type: "string",
						required: true,
						message: "请填写账号",
						trigger: ["blur", "change"],
					},
					password: {
						type: "string",
						required: true,
						message: "请填写密码",
						trigger: ["blur", "change"],
					},
					passWordAgain: [{
							type: "string",
							required: true,
							message: "请确认新密码",
							trigger: ["blur", "change"],
						},
						{
							validator: (rule, value, callback) => {
								return value === that.model.password;
							},
							message: "两次密码不一样，请修改！",
						},
					],
				},
				model: {
					formerPassword: "",
					password: "",
					passWordAgain: "",
				},
				userInfo: {},
			};
		},
		onReady: function() {
			this.userInfo = getUserInfo();
		},
		mounted() {},
		methods: {
			submit() {
				const that = this;
				that.$refs.uForm
					.validate()
					.then((res) => {
						fetch("post", "/basic/user/updatePwd", {
							newPwd: that.model.password,
							oldPwd: that.model.formerPassword,
						}).then((res) => {
							uni.$emit("openToast", {
								type: "success",
								title: "修改成功",
								complete() {
									setTimeout(() => {
										uni.navigateBack({
											delta: 1,
										});
									}, 1000);
								},
							});
						});
					})
					.catch((errors) => {
						uni.$u.toast("校验失败");
					});
			},
		},
	};
</script>

<style scoped lang="scss">
	.login-box {
		position: relative;
		padding-top: 150rpx;
		height: 100vh;
		background: url(~@/static/login-bj.jpg) no-repeat;
		display: flex;
		flex-direction: column;
		background-size: cover;

		.tip {
			padding: 0 54rpx;
			height: 93rpx;
			margin-bottom: 67rpx;

			.dt {
				font-size: 36rpx;
				font-weight: 600;
				margin-bottom: 27rpx;
			}

			.dd {}
		}
	}

	.log {
		position: absolute;
		left: 58rpx;
		top: 40rpx;
		height: 66rpx;
		width: 50%;
	}

	.from-box {
		flex: 1;
		padding: 57rpx 60rpx;
		background-color: #ffffff;
		border-radius: 20px 20px 0 0;

		/deep/ .u-form-item__body__left__content__label {
			font-size: 34rpx;
			margin-bottom: 25rpx;
		}

		/deep/ .u-input {
			background-color: #f2f2f2;
			height: 80rpx;
			border-radius: 10px;
		}
	}

	.but-box {
		width: 100%;
		margin-top: 159rpx;
		display: flex;
		justify-content: space-between;

		.btn {
			flex: 1;

			&:first-child {
				margin-right: 40rpx;
			}
		}
	}

	.code-box {
		display: flex;
		width: 100%;

		.img {
			width: 200rpx;
			height: 80rpx;
			margin-left: 10px;
		}
	}
</style>